<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<title>Connect to database (procedural)</title>
<style>
.container-fluid {
margin-top: 50px;
}
a {
margin: 0px 0px 20px 50px;
}
h1 {
color: purple;
}
h3 {
color: brown;
}
.red {
color: red;
}
</style>
</head>
<body>
<div class="container-fluid">
<a class="btn btn-primary btn-lg" href="showphpsource.php?fileName=<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" role="button" target="_blank">Only PHP Source</a>
<a class="btn btn-primary btn-lg" id="fullSource" href="showfullsource.php?fileName=<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" role="button" target="_blank">Full Source</a>
<h1>Connect to database (procedural):</h1>
<h3>Three <span class="red">IMPORTANT</span> step on <span class="red">cPanel</span> to create database and connecting:</h3>
<ol>
<li>Create <span class="red">new database on cPanel</span>. (NOT on phpMyAdmin!)</li>
<li>Add <span class="red">New User</span> (with password).</li>
<li><span class="red">Add User To Database</span> with full privilegs.</li>
</ol>
<h3>Four variables need to connect your database:</h3>
<ol>
<li>host: <span class="red">"localhost"</span></li>
<li>username: <span class="red">"csabaoff_lendoo"</span> cPanel -> MySql Databases -> Current Users (bottom of page): = You created at step 2. above</li>
<li>password: <span class="red">"myPassword"</span> You created at step 2 above -> If you forget it create another one at Current Users (bottom of page) -> Actions = Change Password</li>
<li>name of database: <span class="red">"csabaoff_users"</span> You find the name at 3. section on the page: Current Databases</li>
</ol>
<?php
// mysqli_connect(host, username, password, name of database)
// connection to my XAMPP server:
// $link = mysqli_connect("localhost", "root", "", "hkk");
// connection to thecompletewebhosting.com server mySQL database:
// $link = mysqli_connect("localhost", "csabaoff_lendoo", "myPassword", "csabaoff_users");
function connection($database, $to = "XAMPP") {
$host = "localhost";
if ($to == "XAMPP") {
$username = "root";
$password = "";
} else {
$username = "csabaoff_lendoo";
$password = "myPassword";
}
if (!$connection = @mysqli_connect($host, $username, $password, $database)) {
die ("ERROR: Unable to connect to the $database database: <br />"
. mysqli_connect_error() . "<br />Error number: "
. mysqli_connect_errno() . "<br />" . "File: " . __FILE__ .
"<br />Line: " . __LINE__);
} else {
return $connection;
}
}
// Connection with function:
// connection to my XAMPP server:
// $connection = connection("hkk"); // with one variable only or with "XAMPP"
$connection = connection("csabaoff_users", "Here can write anythings except XAMPP");
var_dump($connection);
// To check the function work well:
if (mysqli_connect_error()) {
die ("ERROR: Unable to connect: " . mysqli_connect_error() . "<br />Error number: " . mysqli_connect_errno());
}
echo "<p>Connected successfully to the database.</p>";
if ($connection !== "") {
mysqli_close($connection);
}
?>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
</script>
</body>
</html>